<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加角色</title>
    <link rel="stylesheet" href="{{ asset('layui/css/layui.css') }}">
    <script src="{{ asset('layui/layui.js') }}"></script>
    <style>
        body {
            padding: 20px;
        }
        .layui-form-item {
            margin-bottom: 15px;
        }
        .permission-list {
            max-height: 300px;
            overflow-y: auto;
            padding: 10px;
            border: 1px solid #e6e6e6;
            border-radius: 2px;
        }
        .permission-group {
            margin-bottom: 10px;
        }
        .permission-group-title {
            font-weight: bold;
            margin-bottom: 5px;
        }
        .permission-item {
            margin-bottom: 5px;
            margin-left: 20px;
        }
    </style>
</head>
<body>
    <form class="layui-form" lay-filter="roleForm">
        @csrf
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">权限设置</label>
            <div class="layui-input-block">
                <div class="permission-list">
                    @php
                        // 按HTTP路径分组权限
                        $groupedPermissions = [];
                        foreach($permissions as $permission) {
                            $path = $permission->http_path ?: '其他';
                            if (!isset($groupedPermissions[$path])) {
                                $groupedPermissions[$path] = [];
                            }
                            $groupedPermissions[$path][] = $permission;
                        }
                    @endphp
                    
                    @foreach($groupedPermissions as $group => $items)
                        <div class="permission-group">
                            <div class="permission-group-title">{{ $group }}</div>
                            @foreach($items as $permission)
                                <div class="permission-item">
                                    <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" title="{{ $permission->name }} ({{ $permission->slug }})" lay-skin="primary">
                                </div>
                            @endforeach
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="roleForm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.jquery;
            
            // 表单提交
            form.on('submit(roleForm)', function(data) {
                $.ajax({
                    url: '{{ route('admin.auth.roles.store') }}',
                    type: 'POST',
                    data: data.field,
                    success: function(res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                // 关闭弹窗并刷新父页面
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function(xhr) {
                        // 处理验证错误
                        if (xhr.status === 422) {
                            var errors = xhr.responseJSON.errors;
                            var errorMsg = '';
                            
                            for (var key in errors) {
                                errorMsg += errors[key][0] + '<br>';
                            }
                            
                            layer.msg(errorMsg, {icon: 2});
                        } else {
                            layer.msg('服务器错误，请稍后重试', {icon: 2});
                        }
                    }
                });
                
                return false;
            });
        });
    </script>
</body>
</html> 